﻿#include "ScreenCapture.h"
#include <QScreen>
#include <QApplication>
#include <QDesktopWidget>

namespace Common {

	QImage ScreenCapture::getFullScreenImage(ScreenType type, QWidget *widget)
	{
		int index = QApplication::desktop()->screenNumber(widget);
		QScreen *screen = QApplication::screens()[index];
		QImage image;

		switch (type)
		{
		case ScreenCapture::CURRENT_SCREEN:
		{
			image = screen->grabWindow(0).toImage();
		}
		break;
		case ScreenCapture::CURRENT_WINDOW:
		{
			image = screen->grabWindow(widget->winId()).toImage();
		}
		break;
		default:
			break;
		}

		return image;
	}

	QImage ScreenCapture::getScreenImage(ScreenType type, QWidget * widget, int startX, int startY, int width, int height)
	{
		int index = QApplication::desktop()->screenNumber(widget);
		QScreen *screen = QApplication::screens()[index];
		int widthMax, heightMax, winId;

		switch (type)
		{
		case ScreenCapture::CURRENT_SCREEN:
		{
			widthMax = screen->size().width();
			heightMax = screen->size().height();
			winId = 0;
		}
		break;
		case ScreenCapture::CURRENT_WINDOW:
		{
			widthMax = widget->width();
			heightMax = widget->height();
			winId = widget->winId();
		}
		break;
		default:
			break;
		}

		startX = qBound(0, startX, widthMax);
		startY = qBound(0, startY, heightMax);
		width = qBound(0, width, widthMax - startX);
		height = qBound(0, height, heightMax - startY);
		QImage image = screen->grabWindow(winId, startX, startY, width, height).toImage();

		return image;
	}

}//namespace Common 

